package com.msf.tutorial.java8.chapter3;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * 使用函数式接口来传递行为
 */
public class step2 {

    @FunctionalInterface
    public interface BufferedReaderProcessor {
        String process(BufferedReader b) throws IOException;
    }

    private static String processFile(BufferedReaderProcessor p) throws IOException {
        // 使用了Java 7中的带资源的try语句，因此不需要显式地关闭资源了
        try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
            return p.process(br);
        }
    }

    public static void main(String[] args) throws IOException {
        // 读一行
        processFile(br -> br.readLine());

        // 读两行
        processFile(br -> br.readLine() + br.readLine());
    }
}
